# -*- coding: utf-8 -*-
"""
简化多语言支持 - 不依赖 Flask-Babel
提供基本的多语言功能
"""
from flask import Flask, request, session
from .lan import LANGUAGES,TRANSLATIONS,init_lan
from .decorator.ext_decorator import ext_decorator
@ext_decorator
def init_simple_i18n(app: Flask) -> None:
    """初始化简化多语言支持"""
    init_lan()
    # 添加模板全局函数
    @app.context_processor
    def inject_i18n_functions():
        return {
            '_': gettext,
            'ngettext': ngettext,
            'get_locale': get_locale,
            'LANGUAGES': LANGUAGES
        }

def get_locale():
    """获取当前语言"""
    # 优先从session获取
    if 'lang' in session:
        return session['lang']
    # 从请求参数获取
    if request.args.get('lang'):
        return request.args.get('lang')
    
    # 默认返回中文
    return 'zh'

def gettext(text):
    """获取翻译文本"""
    locale = get_locale()
    return TRANSLATIONS.get(locale, TRANSLATIONS['zh']).get(text, text)

def ngettext(singular, plural, n):
    """获取复数形式翻译"""
    if n == 1:
        return gettext(singular)
    else:
        return gettext(plural)



# 导出函数
__all__ = ['gettext', 'ngettext', 'get_locale', 'LANGUAGES', 'init_simple_i18n'] 